import axios from "axios";

function request(config) {
  const instance = axios.create({
    baseURL: "",
  });
  return instance(config);
}

export function login(username, password) {
  return request({
    url: "/api/user/login",
    method: "post",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    params: {
      username,
      password,
      type: 0,
    },
  });
}

export function logout(token) {
  return request({
    url: "/api/user/logout",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
  });
}

//获取院系信息
export function getDepartments(token) {
  return request({
    url: "/api/user/departments",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
  });
}
//获取专业信息
export function getMajors(token, department) {
  return request({
    url: "/api/user/majors",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
    params: {
      department,
    },
  });
}
//获取班级信息
export function getClass(token, department, major) {
  return request({
    url: "/api/user/className",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
    params: {
      department,
      major,
    },
  });
}
//禁用或启用管理员权限
export function toggleAdminPower(token, id, status) {
  return request({
    url: "/api/admin/forbid",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
    params: {
      id,
      status,
    },
  });
}
//管理员查看用户打卡情况
export function clock(token, data, isFinished, pageNum, date) {
  return request({
    url: "/api/clock",
    method: "post",
    headers: {
      Authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    data: {
      ...data,
    },
    params: {
      isFinished,
      pageNum,
      pageSize: 10,
      date,
    },
  });
}
//获取所有普通管理员账号
export function getAllAdmin(token) {
  return request({
    url: "/api/allAdmin",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
  });
}
//增加管理员
export function addAdmin(token, username, password) {
  return request({
    url: "/api/admin",
    method: "post",
    headers: {
      Authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    data: {
      username,
      password,
    },
  });
}
//文件上传学生账号信息
export function uploadAccount(token, data) {
  console.log(token);
  console.dir(data);
  return request({
    url: "/api/upload/account",
    method: "post",
    headers: {
      Authorization: "bearer " + token,
      "Content-Type": "multipart/form-data",
    },
    data,
  });
}
//查看学生账号信息
export function getAccounts(token, pageNum) {
  return request({
    url: "/api/user/accounts",
    method: "post",
    headers: {
      Authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    params: {
      pageNum,
      pageSize: 10,
    },
  });
}

//下载打卡的人员名单
export function downloadClockAsExcel(token, data, isFinished, date) {
  return request({
    url: "/api/clock/download",
    method: "post",
    headers: {
      authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    responseType: "arraybuffer",
    data: {
      ...data,
    },
    params: {
      isFinished,
      date,
    },
  });
}

//导出所有打卡的数据
export function downloadAllQuest(token, date) {
  return request({
    url: "/api/download/quest/all",
    method: "get",
    headers: {
      authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    responseType: "arraybuffer",
    params: {
      date,
    },
  });
}

//导出健康风险数据
export function downloadRiskQuest(token, date) {
  return request({
    url: "/api/download/quest/risk",
    method: "get",
    headers: {
      authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    responseType: "arraybuffer",
    params: {
      date,
    },
  });
}
//导出异常数据
export function downloadExceptionQuest(token, date) {
  return request({
    url: "/api/download/quest/exception",
    method: "get",
    headers: {
      authorization: "bearer " + token,
      "Content-Type": "application/json;charset=utf-8",
    },
    responseType: "arraybuffer",
    params: {
      date,
    },
  });
}

//获取统计信息
export function getDepartmentRatio(token, date) {
  return request({
    url: "/api/stat/ratio",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
    params: {
      date,
    },
  });
}
//获取统计信息
export function getDepartmentNumVary(token) {
  return request({
    url: "/api/stat/numVary",
    method: "get",
    headers: {
      Authorization: "bearer " + token,
    },
  });
}
